package com.elbis.business.controller;

import java.io.Serializable;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;

import com.elbis.business.entity.Game;
import com.elbis.business.entity.locale.City;
import com.elbis.business.entity.locale.Country;
import com.elbis.business.entity.locale.Language;
import com.elbis.util.HibernateUtil;

public class StaticValuesBean implements Serializable {

	private static final long serialVersionUID = -6970386248133154126L;

	private static final Logger logger = Logger
			.getLogger(StaticValuesBean.class);

	private List<Language> languages;

	private List<Country> countries;

	private List<Game> games;

	public StaticValuesBean() {
		languages = getAllLanguages();
		countries = getAllCountries();
		games = getAllGames();
	}

	public List<Language> getLanguages() {
		return languages;
	}

	public void setLanguages(List<Language> languages) {
		this.languages = languages;
	}

	public List<Country> getCountries() {
		return countries;
	}

	public void setCountries(List<Country> countries) {
		this.countries = countries;
	}

	public List<Game> getGames() {
		return games;
	}

	public void setGames(List<Game> games) {
		this.games = games;
	}

	public Country getCountryByName(String name) {
		if (name == null || name.isEmpty()) {
			return null;
		}
		for (Country country : countries) {
			if (country.getName().equals(name)) {
				return country;
			}
		}
		return null;
	}

	public City getCityByName(String name) {
		if (name == null || name.isEmpty()) {
			return null;
		}
		for (Country country : countries) {
			City city = country.getCity(name);
			if (city != null) {
				return city;
			}
		}
		return null;
	}

	public Language getLanguageByName(String name) {
		if (name == null || name.isEmpty()) {
			return null;
		}

		for (Language language : languages) {
			if (language.getName().equals(name)) {
				return language;
			}
		}

		return null;
	}

	public Game getGameByName(String name) {
		if (name == null || name.isEmpty()) {
			return null;
		}
		for (Game game : games) {
			if (game.getName().equals(name)) {
				return game;
			}
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	private List<Country> getAllCountries() {
		Session session = HibernateUtil.getSession();
		List<Country> countries = null;
		try {
			countries = (List<Country>) session.createCriteria(Country.class)
					.list();
		} catch (HibernateException e) {
			logger.error(e.getMessage());
		} finally {
			session.close();
		}
		return countries;
	}

	@SuppressWarnings("unchecked")
	private List<Language> getAllLanguages() {
		Session session = HibernateUtil.getSession();
		List<Language> langs = null;
		try {
			langs = (List<Language>) session.createCriteria(Language.class)
					.list();
		} catch (HibernateException e) {
			logger.error(e.getMessage());
		} finally {
			session.close();
		}
		return langs;
	}

	@SuppressWarnings("unchecked")
	private List<Game> getAllGames() {
		Session session = HibernateUtil.getSession();
		List<Game> gamesList = null;
		try {
			gamesList = (List<Game>) session.createCriteria(Game.class).list();
		} catch (HibernateException e) {
			logger.error(e.getMessage());
		} finally {
			session.close();
		}
		return gamesList;
	}

}
